package com.fan.shop.views.bean;

import java.util.HashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.fan.shop.model.commonbrowser.CommonBrowser;
import com.fan.shop.services.CategoryService;
import com.fan.shop.services.UserService;

@ManagedBean(name = "cbTestBean")
@RequestScoped
public class CBTestBean  extends ShopBean{

	@ManagedProperty(value = "#{userService}")
	private UserService userService;
	@ManagedProperty(value = "#{categoryService}")
	private CategoryService categoryService;

	private String serviceName;
	private String userName;
	private String categoryName;

	public CBTestBean() {
		super();
		setReturnedValues();

	}

	public void setReturnedValues() {
		Map<String, Object> map = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestMap();
		if (map.containsKey("common_browser")) {
			CommonBrowser commonBrowser = (CommonBrowser) map
					.get("common_browser");
			
			
			userName = commonBrowser.getSourceMap().get("user_name").toString();
			categoryName = commonBrowser.getSourceMap().get("category_name").toString();
			
			if(commonBrowser.getTriggerField().equals("user_name"))
			{
				userName = commonBrowser.getReturnList().get(0);
			}
			
			if(commonBrowser.getTriggerField().equals("category_name"))
			{
				categoryName = commonBrowser.getReturnList().get(0);
			}
			
			
			
			
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getUserName() {
		setReturnedValues();
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getCategoryName() {
		setReturnedValues();
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String openUsersAction() {

		CommonBrowser commonBrowser = new CommonBrowser();
		commonBrowser.setTriggerField("user_name");
		commonBrowser.setParentPage("../../views/cbtest.faces");
		commonBrowser.setSelectionMode("single");
		commonBrowser.setShopService(userService);
		commonBrowser.setSourceMap(buildPageMap());

		FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
				.put("common_browser", commonBrowser);
		return "commonbrowser/commonbrowser.faces";
	}

	public String openCategoriesAction() {

		CommonBrowser commonBrowser = new CommonBrowser();
		commonBrowser.setTriggerField("category_name");
		commonBrowser.setParentPage("../../views/cbtest.faces");
		commonBrowser.setSelectionMode("single");
		commonBrowser.setShopService(categoryService);
		commonBrowser.setSourceMap(buildPageMap());

		FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
				.put("common_browser", commonBrowser);
		return "commonbrowser/commonbrowser.faces";
	}

	public Map<String, Object> buildPageMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name", userName);
		map.put("category_name", categoryName);
		return map;
	}

}
